<template>
  <div class="npstyle"> <!--  style="min-height:100vh" -->

    <!-- 待付款头部金额 -->
    <section class="section_status">
      <div class="section_top">
        <p>{{item.DURGSTORENAME}}</p>
        <p>
          ￥{{totalmoney}}
        </p>
        <p>待付款</p>
      </div>

      <!-- 订单号与时间 -->
      <div class="section_con_title">
        <p>订单号：{{item.PRESCRIBEID}}</p>
        <p>{{item.OPENTIME}}</p>
      </div>

    </section>


    <!-- 处方订单内容 -->
    <section class="section_con">



      <!-- 处方基本信息 -->
      <com-preinfo :preinfostr='item' ></com-preinfo>

      <!-- 是否医保结算 -->
      <com-ybjs v-show="showYbInfo" :preinfostr='item' @setIfyb="setIfyb($event)"></com-ybjs>

      <!-- 是否需要配送 -->
      <com-addr :preinfostr='item' @setMedicpsf="setMedicpsf($event)"></com-addr>

      <!-- 需要煎药服务 -->
      <com-jyff :preinfostr='item' @setMedicjgf="setMedicjgf($event)"></com-jyff>

      <!-- 就诊留言 -->
      <com-jzly :preinfostr='item' @setMedicly="setMedicly($event)" ></com-jzly>

    </section>

    <!-- 费用总计 -->
    <section class="section_hj">
      <div class="fyhj">
        <div>
          <span>费用总计：</span>
          <span>￥{{totalmoney}}</span>
        </div>
        <div>
          <span>
            <span>处方费：￥</span><i>{{item.PRESCRIPTION_PRICE}}</i>
          </span>
          &nbsp;|&nbsp;
          <span>
            <span>配送费：￥</span><i>{{psinfoJson.psf}}</i>
          </span>
          &nbsp;|&nbsp;
          <span>
            <span>煎药费：￥</span><i>{{jyffinfoJson.jyxj}}</i>
          </span>
        </div>
      </div>
    </section>


    <!-- 按扭 -->
    <section class="section_bot">

<!--       <nuxt-link class="pay"
       :to="{path:'/chufang/pay',query:{prescribeInfo: JSON.stringify(item), selectedADDRALIAS:this.selectedADDRALIAS}}" tag="div">付款</nuxt-link>
      <div class="pay1" v-else @click="addshaddr">请先添加收货地址</div> -->

      <div :class="true ? 'pay' : 'pay1'" @click="payMoney(item)">付  款</div>

    </section>


  </div>
</template>
<script>
import comPreinfo from '~/pages/chufang/components_yb/com_yb_preinfo';
import comAddr from '~/pages/chufang/components_yb/com_yb_addr';
import comYbjs from '~/pages/chufang/components_yb/com_yb_ifybjs';
import comJyff from '~/pages/chufang/components_yb/com_yb_ifjyff';
import comJzly from '~/pages/chufang/components_yb/com_yb_jzly';

import { MessageBox } from 'mint-ui';

const { config } = window;

export default {
  data() {
    return {

      showYbInfo: config.YBMODE,

      psinfoJson: { // 由收货地址子组件传来的收货地址信息
        shdzid: '',
        kdfw: false,
        shr: '',
        shrdh: '',
        shddz: '',
        psf: 0.00,
        servertype: 'ps'
      },

      ybinfoJson: { // 由医保子父母传来的信息
        ybCardId: '',
        ybCardName: '',
        ybCardNum: '',
        ifybjs: false
      },
      ifybjs: false,  // 是否医保结算
      ybNotice: '您选择医保结算，处方将提交到医保审核结算，请耐心等待...',

      jyffinfoJson: { // 由煎药服务子父母传来的信息
        jyfw: false,
        jydj: 1.00, // 煎药单价
        cfjs: 0,    // 处方剂数
        jyxj: 0.00,
        servertype: 'jy'
      },

      wonote: '', // 患者留言

      showPrescribeDetails: false, // 显示处方详情的标志
      prescribeDetailsInfo: '查看处方详情', // 查看处方详情按扭
      headTitle: '处方订单详情',

      ts: this.newStore(),
      tq: this.getData(),
      
      item: {}, // 当前页面的处方订单对象

    };
  },
  components: {
    comPreinfo, comAddr, comYbjs, comJyff, comJzly
  },
  created() {
    document.title = '处方待付款';
    console.log(this.$route.query.preinfostr);
    //this.item = JSON.parse(this.$route.query.preinfostr);

    this.getOrderDetail();

  },
  mounted() {
  },
  computed: {

    totalmoney() {
      return this.decimal(Number(this.item.PRESCRIPTION_PRICE) + Number(this.jyffinfoJson.jyxj) + Number(this.psinfoJson.psf), 2);
    },

  },
  methods: {
    /**
     * 格式化数值
     * @num 要处理的数字
     * @v 要保留的小数位数
     */ 
    decimal(num, v) {
      var vv = Math.pow(10, v);
      return Math.round(num*vv)/vv;
    },
    // 获取处方订单详情
    getOrderDetail() { // ORDERID

      const oInfo = JSON.parse(this.$route.query.preinfostr);

      const DATA = {
        orderInfo: oInfo.OI_ID,
      };

      this.tq.getOrderDetail(DATA, this).then((ts) => {
        console.log("===============AAAAAAAA==================");
        console.log(JSON.stringify(ts.d));

        this.item = ts.d.DATAOBJ.DATA;

        // 根据订单状态进行跳转
        if (this.item.OI_PAYYBTYPE == 'Y') { // 医保

          const preinfostr = {
            ORDERID: oInfo.OI_ID
          };

          this.$router.push({
            path: '/chufang/ybjs', 
            query:  {preinfostr: JSON.stringify(preinfostr)}
          });

        } else if (this.item.OI_PAYYBTYPE == 'N') { // 自费

          const preinfostr = {
            ORDERID: oInfo.OI_ID
          };

          this.$router.push({
            path: '/chufang/pay', 
            query: {preinfostr: JSON.stringify(preinfostr)}
          });

        }


      });


    },
    payMoney(item) {

      console.log("ps=================" + JSON.stringify(this.psinfoJson));
      if (this.psinfoJson.kdfw) {
        if (this.psinfoJson.shdzid == '' || this.psinfoJson.shdzid == null || this.psinfoJson.shdzid == undefined) {
          this.$messagebox('提示', '请先设置收货地址！');
          return;
        }
      }

      console.log("yb=================" + JSON.stringify(this.ybinfoJson));
      if (this.ybinfoJson.ifybjs) {
        if (this.ybinfoJson.ybCardId == '' || this.ybinfoJson.ybCardId == null || this.ybinfoJson.ybCardId == undefined) {
          this.$messagebox('提示', '请先添加医保信息！');
          return;
        }
      }

/*       
      console.log("jyff=================" + JSON.stringify(this.jyffinfoJson));
      alert(JSON.stringify(this.jyffinfoJson));
      if (this.jyffinfoJson.jyfw) {
        if (this.jyffinfoJson.jyxj == '' || this.jyffinfoJson.shdzid == jyxj || 
            this.jyffinfoJson.jyxj == undefined || this.jyffinfoJson.jyxj == 0.00) {
          this.$messagebox('提示', '煎药服务设置有误！');
          return;
        }
      } */


      if (this.ifybjs) {
        MessageBox.confirm(this.ybNotice, '医保结算提示').then(
          action => { // 确认
            this.goPayMoney(item, '/chufang/ybjs');
        }, action => { // 取消
        });
      } else {
        this.goPayMoney(item, '/chufang/pay');
      }
    },
    goPayMoney(item, url) {

      let orderMx = [];
      console.log("kdfw ------------------------------------");
      console.log(this.psinfoJson.kdfw);
      console.log(this.psinfoJson.servertype);
      if (this.psinfoJson.kdfw) {
        let fw = {
          om_oi_id: this.item.OI_ID,
          om_charge_content: '快递服务',
          om_charge_je: this.psinfoJson.psf,
          om_charge_type: 'ps'
        };
        orderMx.push(fw);
      }
      
      console.log("jyff ------------------------------------");
      console.log(this.jyffinfoJson.jyfw);
      console.log(this.jyffinfoJson.servertype);
      if (this.jyffinfoJson.jyfw) {
        let jy = {
          om_oi_id: this.item.OI_ID,
          om_charge_content: '煎药服务',
          om_charge_je: this.jyffinfoJson.jyxj,
          om_charge_type: 'jy'
        };
        orderMx.push(jy);
      }
      let presInfo = {
        qytype: this.psinfoJson.kdfw ? 'kd' : 'zq',
        ifybjs: this.ifybjs ? 'y' : 'n',
        ybjszt: 'o',  // o结算中（无论是否医保，提交后就处于结算中状态），k已结算'
        pres_id: this.item.OI_BUSID
      };
      let orderInfo = {
        oi_id: this.item.OI_ID,
        oi_qjdz: this.psinfoJson.shrdz,
        oi_oa_lxrxm: this.psinfoJson.shr,
        oi_oa_lxrsj: this.psinfoJson.shrdh,
        oi_payybtype: this.ifybjs ? 'y' : 'n',
        oi_je_pay: this.totalmoney,
        oi_wonote: this.wonote,
        detail: orderMx
      };
      const DATA = {
        presInfo,
        orderInfo
      };

      console.log("============保存订单前的JSON============");
      console.log(JSON.stringify(DATA));
      
      this.tq.doPay(DATA, this).then((ts) => {
        console.log("=================================");
        console.log(ts.d);

        // 设置处方订单的费用
        item.zje = this.totalmoney;                 // 总金额
        item.jgf = this.jyffinfoJson.jyxj;          // 加工费
        item.psf = this.psinfoJson.psf;             // 配送费

        // 设置医保信息
        item.OI_PAYYBTYPE = this.ifybjs ? 'y' : 'n';

        // 保存订单信息后，根据是否医保情况跳转
        const re = '200';
        if (re == '200') {
          this.$router.push({
            path:url,
            query:{
              preinfostr: JSON.stringify(item)
            }
          });
        } else {
          this.$messagebox('付款提示', '提交付款请求出现错误，请重试');
        }

      });



    },
    setIfyb(ybinfo) {
      console.log('nonePayment.ybinfo=========' + ybinfo);
      this.ybinfoJson = JSON.parse(ybinfo);
      this.ifybjs = this.ybinfoJson.ifybjs;
    },
    setMedicjgf(jyffinfo) {
      console.log('nonePayment.jyffinfo=========' + jyffinfo);
      this.jyffinfoJson = JSON.parse(jyffinfo);
    },
    setMedicpsf(psinfo) {
      console.log('nonePayment.psinfo=========' + psinfo);
      this.psinfoJson = JSON.parse(psinfo);
    },
    setMedicly(lyinfo) {
      console.log('nonePayment.lyinfo=========' + lyinfo);
      this.wonote = lyinfo;
    },
    showPrescribe() {
      this.showPrescribeDetails = true;
    },
  },
};
</script>
<style scoped>
/* GLOBLE */
* {
  margin: 0;
  padding: 0;
}

.npstyle {
  display: flex;
  flex-direction: column;
  background: #f1f1f1;
}

.section_hj {
  margin-bottom: 4rem;
}


/* SECTION */
.section_status {
  position: fixed;
  width: 100%;
  margin-top: -0.5rem;
  height: auto;
  padding-bottom: 1rem;
  background: #fff;
  -webkit-transform: translate3d(5, -50%, 0);
  transform: translate3d(5, -50%, 0);
  -webkit-box-shadow: 0px -3px 10px #ddd;
  -moz-box-shadow: 0px -3px 10px #ddd;
  box-shadow: 0px -3px 10px #ddd;
  z-index: 999;
}
.section_top p {
  text-align: center;
  color: #333;
  font-size: 2rem;
  font-family: "微软雅黑";
  margin: 0;
  padding-top: 1rem;
}
.section_top p:first-child {
  font-size: 1rem;
  font-family: "微软雅黑";
}
.section_top p:nth-child(2) {
  color: #000;
}
.section_top p:last-child {
  font-size: 1rem;
  font-family: "微软雅黑";
  padding-top: .5rem;
  color: #ccc; /* 26a2ff */
}
.section_con {
  margin-top: 8.5rem;
  font-size:1rem;
}
.section_con p {
  color: #333;
  font-family: "微软雅黑";
}
.section_con div {
  margin-top: 0.5rem;
  background: #fff;
  padding: 0.5rem;
}
.section_con div:first-child {
  margin-top: 0;
}
.section_con_title {
  display: flex;
  flex-flow: row;
  justify-content: space-between;
  align-items: center;
  font-size: .8rem;
  color: #666;
  margin: 1rem auto 0 auto;
  width: 95%;
}
.section_con_title p{
  color: #ccc;
}
.section_bot {
  /* background: #fff; */
  margin: 0.5rem 0;
  font-size: 0.9rem;
  position: fixed;
  bottom: 0;
  width: 100%;
  margin-top: 1rem;
}
.section_bot span:nth-child(2) {
  float: right;
  color: #f60;
}
.section_bot .pay {
  width: 95%;
  height: 2.5rem;
  line-height: 2.5rem;
  background-color: #26a2ff;
  color: #fff;
  text-align: center;
  vertical-align: middle;
  font-weight: 100;
  letter-spacing: 2rem;
  margin: 0 auto;
  border-radius: 10px;
  padding-left: 2rem;
}
.section_bot .pay1 {
  width: 95%;
  padding: 8px;
  background-color:#ccc;
  border-color: #357ebd;
  color: #fff;
  border-radius: 0.2rem;
  text-align: center;
  vertical-align: middle;
  border: 1px solid transparent;
  /* font-weight: 700; */
  letter-spacing: .1rem;
  margin: 0 auto;
  border-radius: 10px;
}

.fyhj {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding-top: 1rem;
  width:95%;
}
.fyhj div:first-child {
  display: flex;
  flex-direction: row;
  align-content: space-between;
}
.fyhj div:first-child span{
  display: inline-flex;
  flex-direction: row;
  align-content: space-between;
  font-size: 1rem;
  font-family: auto;
  margin-bottom: .5rem;
  font-weight: 600;
}
.fyhj div:last-child {
  color: #666;
  font-size: .8rem;
}



/* MINT */
.mint-popup-top {
  top: 50%;
  font-size: 1rem;
  background: none;
  color: #fff;
  letter-spacing: 1px;
}
.mint-switch-core::after{
  width: 22px;
  height: 22px;
}
.mint-switch-core {
  width: 44px;
  height: 25px;
}
.mint-switch-core::before {
  width: 42px;
  height: 23px;
}
</style>
